Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
Why when I pass "q" the second time into my input it does not break the loop?

It works the first time when I pass "q" into my input, but after that, it no longer works. Why? Appreciate any kind advice.

alert("Welcome to your To-Do List!")

let userInput = prompt("What would you like to do?");

let toDo = [];

while (userInput != "q") {
    let userInput = prompt("What would you like to do?");
    console.log(userInput);
}

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Because you're declaring a new userInput variable inside the loop, you need to re-assign to the same variable.

alert("Welcome to your To-Do List!")

let userInput = prompt("What would you like to do?");

let todos = [];

while (userInput != "q") {
  todos.push(userInput);
  userInput = prompt("What would you like to do?");
}

console.log(todos);

about 4 years ago · Juan Pablo Isaza Relatório

0

Because you have defined userInput variable inside the while loop again. This makes it locally scoped for the while loop. Just remove let before the variable userInput in while loop.

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is that third let. It's creating another userInput, scoped to that inner block, which shadows the outer one.

Try changing it to:

alert("Welcome to your To-Do List!")

let userInput = prompt("What would you like to do?");

let toDo = [];

while (userInput != "q") {
    userInput = prompt("What would you like to do?");
    console.log(userInput);
}

You can read more about let here.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda